<template>
  <el-container class="layout-container">
    <el-aside width="200px">
      <el-menu
        :router="true"
        class="el-menu-vertical"
        :default-active="route.path"
        background-color="#fff"
        text-color="#303133"
        active-text-color="#409EFF"
      >
        <!-- 只有管理员可见赛事管理 -->
        <el-menu-item v-if="userStore.role === 'admin'" index="/event">
          <el-icon><Calendar /></el-icon>
          <span>赛事管理</span>
        </el-menu-item>
        
        <!-- 所有角色可见报名管理 -->
        <el-menu-item index="/registration">
          <el-icon><Document /></el-icon>
          <span>报名管理</span>
        </el-menu-item>
        
        <!-- 所有角色可见成绩管理 -->
        <el-menu-item index="/result">
          <el-icon><Trophy /></el-icon>
          <span>成绩管理</span>
        </el-menu-item>
        
        <!-- 只有管理员可见用户管理 -->
        <el-menu-item v-if="userStore.role === 'admin'" index="/user">
          <el-icon><User /></el-icon>
          <span>用户管理</span>
        </el-menu-item>
      </el-menu>
    </el-aside>
    
    <el-container>
      <el-header>
        <div class="header-right">
          <span class="user-info">
            {{ userStore.name }}
            <el-tag size="small" :type="roleType">{{ roleLabel }}</el-tag>
          </span>
          <el-dropdown>
            <span class="el-dropdown-link">
              <el-icon class="el-icon--right"><arrow-down /></el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="handleLogout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>
      
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { useRoute, useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { computed } from 'vue'
import { Calendar, Document, Trophy, User, ArrowDown } from '@element-plus/icons-vue'

const route = useRoute()
const router = useRouter()
const userStore = useUserStore()

// 角色显示标签
const roleLabel = computed(() => {
  const roleMap = {
    admin: '管理员',
    teacher: '教师',
    student: '学生'
  }
  return roleMap[userStore.role] || userStore.role
})

// 角色标签类型
const roleType = computed(() => {
  const typeMap = {
    admin: 'danger',
    teacher: 'warning',
    student: 'info'
  }
  return typeMap[userStore.role] || ''
})

const handleLogout = () => {
  userStore.logout()
  router.push('/login')
}
</script>

<style scoped>
.layout-container {
  height: 100vh;
}

.el-header {
  background-color: #fff;
  border-bottom: 1px solid #dcdfe6;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding: 0 20px;
}

.el-aside {
  border-right: 1px solid #dcdfe6;
  background-color: #fff;
}

.el-menu {
  border-right: none;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
}

.el-menu-item:hover {
  background-color: #f5f7fa !important;
}

.el-menu-item.is-active {
  background-color: #ecf5ff !important;
  color: #409EFF !important;
}
</style> 